package com.cicigodd.brick.plug.security.resource.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cicigodd.brick.plug.common.exception.BusinessException;
import com.cicigodd.brick.plug.common.factory.PageFactory;
import com.cicigodd.brick.plug.common.factory.PageResultFactory;
import com.cicigodd.brick.plug.common.response.PageResult;
import com.cicigodd.brick.plug.security.resource.entity.SysResource;
import com.cicigodd.brick.plug.security.resource.mapper.SysResourceMapper;
import com.cicigodd.brick.plug.security.resource.request.*;
import com.cicigodd.brick.plug.security.resource.response.SysResourceDetailResponse;
import com.cicigodd.brick.plug.security.resource.response.SysResourceListResponse;
import com.cicigodd.brick.plug.security.resource.service.SysResourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统资源
 *
 * @author cicigodd
 * @date 2022/11/15 15:46:30
 */
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements SysResourceService {

    /**
     * 添加
     *
     * @author cicigodd
     * @date 2022/11/15 15:46:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(SysResourceAddRequest sysResourceAddRequest) {
        SysResource sysResource = new SysResource();
        BeanUtil.copyProperties(sysResourceAddRequest, sysResource);
        this.save(sysResource);
    }

    /**
     * 删除
     *
     * @author cicigodd
     * @date 2022/11/15 15:46:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysResourceDeleteRequest sysResourceDeleteRequest) {
        this.removeByIds(sysResourceDeleteRequest.getIds());
    }


    /**
     * 编辑
     *
     * @author cicigodd
     * @date 2022/11/15 15:46:30
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SysResourceEditRequest sysResourceEditRequest) {
        SysResource sysResource = this.getById(sysResourceEditRequest.getId());
        if (ObjectUtil.isEmpty(sysResource)) {
            throw new BusinessException("查询结果不存在");
        }
        BeanUtil.copyProperties(sysResourceEditRequest, sysResource);
        this.updateById(sysResource);
    }

    /**
     * 详情
     *
     * @author cicigodd
     * @date 2022/11/15 15:46:30
     */
    @Override
    public SysResourceDetailResponse detail(SysResourceDetailRequest sysResourceDetailRequest) {
        SysResource sysResource = this.getById(sysResourceDetailRequest.getId());
        if (ObjectUtil.isEmpty(sysResource)) {
            throw new BusinessException("查询结果不存在");
        }
        SysResourceDetailResponse sysResourceDetailResponse = new SysResourceDetailResponse();
        BeanUtil.copyProperties(sysResource, sysResourceDetailResponse);
        return sysResourceDetailResponse;
    }

    /**
     * 列表
     *
     * @author cicigodd
     * @date 2022/11/15 15:46:30
     */
    @Override
    public PageResult<SysResourceListResponse> list(SysResourceListRequest sysResourceListRequest) {
        LambdaQueryWrapper<SysResource> wrapper = new LambdaQueryWrapper<>();
        String resourceCode = sysResourceListRequest.getResourceCode();
        String resourceName = sysResourceListRequest.getResourceName();
        String className = sysResourceListRequest.getClassName();
        String methodName = sysResourceListRequest.getMethodName();
        String url = sysResourceListRequest.getUrl();
        String httpMethod = sysResourceListRequest.getHttpMethod();
        String requiredPermissionFlag = sysResourceListRequest.getRequiredPermissionFlag();

        wrapper.like(ObjectUtil.isNotEmpty(resourceCode), SysResource::getResourceCode, resourceCode);
        wrapper.like(ObjectUtil.isNotEmpty(resourceName), SysResource::getResourceName, resourceName);
        wrapper.like(ObjectUtil.isNotEmpty(className), SysResource::getClassName, className);
        wrapper.like(ObjectUtil.isNotEmpty(methodName), SysResource::getMethodName, methodName);
        wrapper.like(ObjectUtil.isNotEmpty(url), SysResource::getUrl, url);
        wrapper.like(ObjectUtil.isNotEmpty(httpMethod), SysResource::getHttpMethod, httpMethod);
        wrapper.like(ObjectUtil.isNotEmpty(requiredPermissionFlag), SysResource::getRequiredPermissionFlag, requiredPermissionFlag);
        IPage<SysResourceListResponse> page = this.page(PageFactory.defaultPage(sysResourceListRequest.getPageNum(),sysResourceListRequest.getPageSize()), wrapper).convert(SysResource -> BeanUtil.copyProperties(SysResource, SysResourceListResponse.class));
        return PageResultFactory.createPageResult(page);
    }


}
